import React, { createContext, useContext } from "react";
import type { ColumnPlusType } from "./types";

export interface TableContextType {
    selectRows: any[],
    columns: ColumnPlusType<any>[],
    setColumns?: (columns: ColumnPlusType<any>[]) => void
}

export type TableProviderProps<T> = {
    children: React.ReactNode,
    contextValue: T
}

export const TableContext = createContext<TableContextType | undefined>(undefined)

export const TableProvider = <T extends TableContextType>({ children, contextValue }: TableProviderProps<T>) => {
    return (
        <TableContext.Provider value={contextValue}>
            {children}
        </TableContext.Provider>
    )
}

export const useTableContext = () => {
    const context = useContext(TableContext);
    if (!context) {
        throw new Error('useTableContext must be used within a TableProvider');
    }
    return context;
};

